package cn.iocoder.yudao.module.mp.api;

import cn.iocoder.yudao.module.mp.api.qrcode.TempQrcodeApi;
import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
import cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.mp.enums.qrcode.MpQrSceneEnum;
import cn.iocoder.yudao.module.mp.service.account.MpAccountService;
import cn.iocoder.yudao.module.mp.service.qr.MpQrService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
@Slf4j
@Validated
public class TempQrCodeApiImpl implements TempQrcodeApi {

    @Resource
    private MpQrService mpQrService;

    @Resource
    private MpAccountService mpAccountService;

    @Override
    public WxMpQrCodeTicket genTempQrCode(Long accountId, MpQrSceneEnum scene) {
        return mpQrService.generateAccountTempQrCode(accountId, scene);
    }

    @Override
    public WxMpQrCodeTicket genTempQrCode(String appId, MpQrSceneEnum scene) {
        MpAccountDO account = mpAccountService.getAccountFromCache(appId);
        if (account == null) {
            throw exception(ErrorCodeConstants.ACCOUNT_NOT_EXISTS);
        }
        return mpQrService.generateAccountTempQrCode(account.getId(), scene);
    }

    @Override
    public String getTempQrCodeScanOpenid(String ticket) {
        return mpQrService.getQrScanUser(ticket);
    }
}
